package cn.easy.mortgage.api;

import cn.easy.base.bean.common.ApiResultBean;
import cn.easy.mobile.utils.ValidateUtil;
import cn.easy.mobile.web.ApiBaseController;
import cn.easy.mortgage.bean.*;
import cn.easy.mortgage.domain.Channel;
import cn.easy.mortgage.domain.EstateEvalInfo;
import cn.easy.mortgage.domain.LoanRequest;
import cn.easy.mortgage.service.*;
import cn.easy.mortgage.utils.Constants;
import cn.easy.mortgage.utils.SmsUtils;
import freemarker.template.TemplateException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.utils.text.TextValidator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/zxy")
public class ZxyController extends ApiBaseController {
    @Autowired
    private HjPgService hjPgService;
    @Autowired
    private EstateEvalInfoService estateEvalInfoService;
    @Autowired
    private LoanRequestService loanRequestService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private ChannelService channelService;
    @Autowired
    private LvXinTongSmsService lvXinTongSmsService;

    @RequestMapping(value = "/getArea", method = RequestMethod.GET)
    @ResponseBody
    public ApiResultBean<? extends Object> getAreas(HttpServletRequest request, String parentCode, String codeType) {
        return toSuccess("查询成功", commonService.getAreaList());
    }

    @RequestMapping(value = "/getPropertyType", method = RequestMethod.GET)
    @ResponseBody
    public ApiResultBean<? extends Object> getPropertyType(HttpServletRequest request) {
        List<SelectBean> result = new ArrayList<>();
        for (Map.Entry<String, String> entry : getConfigMap(Constants.PROPERTY_TYPE_KEY).entrySet()) {
            result.add(new SelectBean(entry.getValue(), entry.getKey()));
        }
        return toSuccess("查询成功", result);
    }

    @RequestMapping(value = "/getHouseAddress", method = RequestMethod.GET)
    @ResponseBody
    ApiResultBean<? extends Object> gethouseAddress(String area, String propertyType, String addr, HttpServletRequest request) {
        HjPGSearchHouseBean hjPGSearchHouseBean = new HjPGSearchHouseBean(addr, propertyType, area);
        HjPGSearchHouseResultBean hjPGSearchHouseResultBean = hjPgService.searchHouse(hjPGSearchHouseBean);
        List<HjPGSingleQueryBean> hjPGSingleQueryBeanList = hjPGSearchHouseResultBean.getSingleQueryBeanList();
        for (HjPGSingleQueryBean hjPGSingleQueryBean : hjPGSingleQueryBeanList) {
            hjPGSingleQueryBean.setPropertyType(propertyType);
        }
        return toSuccess("查询成功", hjPGSingleQueryBeanList);
    }

    @RequestMapping(value = "/getCode", method = RequestMethod.POST)
    @ResponseBody
    ApiResultBean<? extends Object> getCode(String mobile, HttpServletRequest request, HttpServletResponse response) {
        ValidateUtil.checkExpire(SmsUtils.CODE_MAP, 1800);
        if (!TextValidator.isMobileSimple(mobile)) {
            return toError("手机号格式不正确", 101);
        }
        if (!ValidateUtil.validCodeSendInterval(SmsUtils.CODE_MAP, mobile, 60)) {
            return toError("验证码已发送，请60秒后重新获取", 102);
        }
        if (!SmsUtils.checkSafe(mobile)) {
            return toError("发送频繁请稍后再试", 103);
        }
        try {
            lvXinTongSmsService.sendCode(SmsUtils.CODE_MAP, mobile, getConfigStr(Constants.CODE_NOTICE_TEMPLATE_KEY));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return toSuccess("验证码已发送");
    }

    @RequestMapping(value = "/toEval", method = RequestMethod.POST)
    @ResponseBody
    ApiResultBean<? extends Object> toEval(HjPGSingleQueryBean hjPGSingleQueryBean, String area, String mobile, String code, Model model, HttpServletRequest request) {
//        if (!BaseUtils.isDev() && !ValidateUtil.checkCode(SmsUtils.CODE_MAP, mobile, code, 900, true)) {
//            return toError("验证码无效", -1);
//        }

        Channel channel = channelService.findByCode("zxy");
        if (channel == null) {
            return toError("未知渠道", -1);
        }

        EstateEvalInfo estateEvalInfo = null;
        if (!estateEvalInfoService.evaluatedOnToday(hjPGSingleQueryBean.getHouseAddress(), hjPGSingleQueryBean.getRoomSize())) {
            HjPGSingleQueryResultBean hjPGSingleQueryResultBean = hjPgService.singleQuery(hjPGSingleQueryBean);
            if (hjPGSingleQueryResultBean == null || StringUtils.isBlank(hjPGSingleQueryResultBean.getFv_house_total_price())) {
                return toError("未查询到评估价格", -1);
            }
            estateEvalInfo = estateEvalInfoService.valueOf(area, hjPGSingleQueryBean, hjPGSingleQueryResultBean);
            estateEvalInfo = estateEvalInfoService.save(estateEvalInfo);
        } else {
            estateEvalInfo = estateEvalInfoService.findByHouseAddressAndRoomSizeOnToday(hjPGSingleQueryBean.getHouseAddress(), hjPGSingleQueryBean.getRoomSize());
        }

        LoanRequest loanRequest = LoanRequest.valueOfZxy(area, mobile, estateEvalInfo, channel);
        loanRequestService.save(loanRequest);
        return toSuccess("查询成功", estateEvalInfo);
    }

}
